/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modele;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.SAXException;

/**
 *
 * @author Fardjana
 */
public class TD extends Sujet {
    private ArrayList<Exercice> mListeExercices = new ArrayList<Exercice>();
    
    public TD(String pSujetXML) throws ParserConfigurationException, SAXException, IOException, JDOMException
    {
        super(pSujetXML);
        buildSujetFromXml(pSujetXML);
    }
    
    public UUID getId()
    {
        return super.getId();
    }
    
        public int getNbExercices()
    {
        return mListeExercices.size();
    }
    
    public Exercice getExercice(int pI)
    {
        return mListeExercices.get(pI);
    }
    
    private void buildSujetFromXml(String pSujetXML) throws ParserConfigurationException, SAXException, IOException, JDOMException
    {
        org.jdom.Document vXML = getXmlDocument(pSujetXML);
        org.jdom.Element vRacine = vXML.getRootElement();
        
        List vListeExercice = vRacine.getChildren("exercice");
        Iterator vIterEx = vListeExercice.iterator();
        
        /* On crée les exercices */
        while(vIterEx.hasNext())
        {
            org.jdom.Element vExerciceCourant = (org.jdom.Element)vIterEx.next();
            
            Exercice exercice = new Exercice(mIdSujet);
            
            String vContenu = "";
            
            org.jdom.Element vIntroduction = vExerciceCourant.getChild("introduction");
            if(vIntroduction != null)
                vContenu += "<introduction>"+vIntroduction.getValue()+"</introduction>";
            
            List vListeQuestion = vExerciceCourant.getChildren("question");
            Iterator vIterQuest = vListeQuestion.iterator();
            
            while(vIterQuest.hasNext())
            {
                org.jdom.Element vQuestionCourante = (org.jdom.Element)vIterQuest.next();
                vContenu += "<question>"+vQuestionCourante.getValue()+"</question>";
            }
            
            org.jdom.Element vAnnotation = vExerciceCourant.getChild("annotation");
            if(vAnnotation != null)
                vContenu += "<annotation>"+vAnnotation.getValue()+"</annotation>";
            
            exercice.setContenu(vContenu);
            mListeExercices.add(exercice);
        }
    }
    
    private org.jdom.Document getXmlDocument(String pSujetXml) throws ParserConfigurationException, SAXException, IOException, JDOMException
    {
        SAXBuilder builder = new SAXBuilder(); 
        org.jdom.Document document = builder.build(new StringReader(pSujetXml));
        return document;
    }
    
    public String tdToXML()
    {
        String vContenuXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><sujet xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"sujet.xsd\">";
        vContenuXml += "<ue>"+mUE+"</ue>";
        vContenuXml += "<numeroTD>"+mNumeroTD+"</numeroTD>";
        vContenuXml += "<titre>"+mTitre+"</titre>";
        vContenuXml += mAuteur;
        
        Iterator<Exercice> vIterEx = mListeExercices.iterator();
        while(vIterEx.hasNext())
        {
            vContenuXml +="<exercice>";
            Exercice exercice = vIterEx.next();
            vContenuXml += exercice.getContenu();
            vContenuXml += "</exercice>";
        }    
        
        vContenuXml += "</sujet>";
        return vContenuXml;
    }
}
